// pages/price/wprice.js
import { wtype,wprice } from '../../api/price'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:'',
    picker:[],
    priceArr:[
      {id:0, name:'前挡',img:'../../img/w1.png',price:'0', picker:[], pickerIndex:-1 },
      {id:1, name:'侧后档',img:'../../img/w2.png',price:'0', picker:[], pickerIndex:-1 },
      {id:2, name:'天窗',img:'../../img/w3.png',price:'0', picker:[], pickerIndex:-1 },
      {id:3, name:'前侧窗',img:'../../img/w4.png',price:'0', picker:[], pickerIndex:-1 },
      {id:4, name:'后侧窗',img:'../../img/w5.png',price:'0', picker:[], pickerIndex:-1 },
      {id:5, name:'后挡',img:'../../img/w6.png',price:'0', picker:[], pickerIndex:-1 },
    ]
  },
  pickerFn(e){
    let { type,picker,priceArr } = this.data
    const pickerIndex = e.detail.value
    const wtype = picker[pickerIndex]
    const { place,id } = e.currentTarget.dataset
    
    priceArr = priceArr.map((item,index)=>{
      console.log(item.name,type)
      if(id===index){
        item.pickerIndex = pickerIndex
      }
      return item
    })
    this.setData({ priceArr })

    wprice({type,wtype,place}).then(res=>{
      let { price } = res.data
      priceArr = priceArr.map((item,index)=>{
        console.log(item.name,type)
        if(id===index){
          item.price = price
        }
        return item
      })
      this.setData({ priceArr })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { type } = options
    this.setData({ type })
    wtype().then(res=>{
      let { priceArr } = this.data
      let picker = []
      let _data = res.data
      _data.map(item=>{
        picker.push(item.name)
      })
      priceArr.map(item=>{
        item.picker = picker
        return item
      })
      this.setData({ priceArr,picker })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})